/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.boot2.core.web;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpResponse;

/**
 * @author zhangweilin
 * @date 2020/7/17 19:21
 * @Description: RestTemplate拦截器处理接口
 */
public interface RestTemplateInterceptor {

    /**
     * 指定的拦截器必备的请求前缀
     * @return
     */
    String getBaseUrl();

     default ClientHttpResponse startIntercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) {
//         HttpServletRequest request1 = RequestContext.getRequest();
//         if (null != request1) {
//             HttpHeaders headers = request.getHeaders();
//             //固定续传的header
//             String xForwardedFor = request1.getHeader("x-forwarded-for");
//             if (CheckUtil.isNotEmpty(xForwardedFor)) {
//                 headers.add("x-forwarded-for", xForwardedFor);
//             }
//             String xRealIP= request1.getHeader("X-Real-IP");
//             if (CheckUtil.isNotEmpty(xRealIP)) {
//                 headers.add("X-Real-IP", xRealIP);
//             }
//             String host = request1.getHeader("Host");
//             if (CheckUtil.isNotEmpty(host)) {
//                 headers.add("Host", host);
//             }
//             String fullUrl = request1.getHeader("fullUrl");
//             if (CheckUtil.isNotEmpty(fullUrl)) {
//                 headers.add("fullUrl", fullUrl);
//             }
//
//             //以下为业务系统配置的需要续传的header
//             List<String> headerList = EnvironmentUtil.get("restTemplate.headers", List.class);
//             if (CheckUtil.isNotEmpty(headerList)) {
//                 for (String header : headerList) {
//                     String header1 = request1.getHeader(header);
//                     if (CheckUtil.isNotEmpty(header1)) {
//                         headers.add(header, header1);
//                     }
//                 }
//             }
//         }
         return intercept(request,body,execution);
    }


    ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution);


}
